package com.ocom.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.common.entity.company.EntityComDockApi;
import com.ocom.common.entity.company.EntityDockApi;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.utils.StringUtil;
import com.ocom.common.vo.company.DockApiListVo;
import com.ocom.common.vo.company.DockApiUrlVo;
import com.ocom.common.vo.company.GetDockApiConfigVo;
import com.ocom.company.dao.IDaoDockApi;
import com.ocom.company.service.ComDockApiService;
import com.ocom.company.service.DockApiService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DockApiServiceImpl extends ServiceImpl<IDaoDockApi, EntityDockApi> implements DockApiService {

    @Autowired
    IDaoDockApi iDaoDockApi;

    @Autowired
    ComDockApiService comDockApiService;

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public List<DockApiUrlVo> getDockApiUrl(String dockCode, Integer grade, Long comId, String serverCode) {
        return iDaoDockApi.getDockApiUrl(dockCode,
                grade,
                comId,
                serverCode);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public List<EntityDockApi> getDockApiByDockCodeAndApiCode(String dockCode, String dockApiCode) {
        QueryWrapper<EntityDockApi> queryWrapper = Wrappers.query();
        queryWrapper.eq("dock_code", dockCode);
        queryWrapper.eq("dock_api_code", dockApiCode);

        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public List<EntityDockApi> getDockApiByDockCodeAndApiName(String dockCode, String apiName) {
        QueryWrapper<EntityDockApi> queryWrapper = Wrappers.query();
        queryWrapper.eq("dock_code", dockCode);
        queryWrapper.eq("api_name", apiName);

        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public List<EntityDockApi> getDockApiByDockCodeAndApiNameAndNotApiCode(String dockCode, String apiName, String dockApiCode) {
        QueryWrapper<EntityDockApi> queryWrapper = Wrappers.query();
        queryWrapper.eq("dock_code", dockCode);
        queryWrapper.eq("api_name", apiName);
        queryWrapper.ne("dock_api_code", dockApiCode);

        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public Boolean update(EntityDockApi entityDockApi) {
        UpdateWrapper<EntityDockApi> updateWrapper = Wrappers.update();
        updateWrapper.lambda()
                .eq(EntityDockApi::getDockCode, entityDockApi.getDockCode())
                .eq(EntityDockApi::getDockApiCode, entityDockApi.getDockApiCode());
        return update(entityDockApi, updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public Map getPageDockApi(String dockCode, Integer page, Integer size, Map<String, Object> parameters) {
        QueryWrapper<EntityDockApi> queryWrapper = Wrappers.query();
        queryWrapper.eq("dock_code", dockCode);
        parameters.forEach((key,value) -> {
            queryWrapper.like(key, value);
        });
        Integer total = this.count(queryWrapper);

        if (null == page || null == size || (page < 0 && size < 0)) {
            page = 0;
            size = 500;
        }
        PageHelper.startPage(page, size);
        List<EntityDockApi> list = this.list(queryWrapper);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("list", list);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public Map getPageDockApiList(String dockCode, String dockApiCode, String apiName, Integer grade, int page, int size) {
        if (page < 0 || size <= 0) {
            page = 0;
            size = 500;
        } else {
            page = page == 0 ? page : (page - 1) * size;
        }
        List<DockApiListVo> list = iDaoDockApi.getDockApiList(dockCode,dockApiCode,apiName,grade,page,size);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("list", list);
        resultMap.put("total",iDaoDockApi.getDockApiList(dockCode,dockApiCode,apiName,grade,0,999).size());
        return resultMap;
    }

    @Override
    public EntityDockApi getDockApi(String dockCode, String dockApiCode, Integer grade, Long comId, String serverCode) {
        QueryWrapper<EntityDockApi> queryWrapper = Wrappers.query();
        queryWrapper.eq("dock_code", dockCode);
        queryWrapper.eq("dock_api_code", dockApiCode);

        queryWrapper.eq("grade ", grade );
        if (10 == grade && StringUtil.isNotEmpty(serverCode)) {
            queryWrapper.eq("server_code ", serverCode );
        }
        if (0 == grade && null != comId) {
            queryWrapper.eq("com_id", comId);
        }
        queryWrapper.last("LIMIT 1");

        return this.getOne(queryWrapper);
    }


}
